<?php

/**
 * @Descripttion : FOXCMS 是一款高效的 PHP 多端跨平台内容管理系统
 * @Author : FoxCMS Team
 * @Date : 2023/6/26   19:22
 * @version : V1.08
 * @copyright : ©2021-现在 贵州黔狐科技股份有限公司 版权所有
 * @LastEditTime : 2023/6/26   19:22
 */

namespace app\plus\controller;

use app\common\model\AccessStat;
use think\Response;

// 统计
class Access
{

    private  $limitTime = 5; //限制时间单位秒

    public function stat()
    {
        $ip = getAccessIP();
        $k = "access_" . str_replace(".", "_", $ip);

        //请求过滤拦截
        $timestamp1 = saveToCache($k);
        if ($timestamp1 != null) {
            $timestampArr = time_diff($timestamp1, time());
            $hours = $timestampArr["hours"]; //小时
            $minutes = $timestampArr["minutes"]; //分钟
            $seconds = $timestampArr["seconds"]; //秒
            if ($hours <= 0 && $minutes <= 0 && $seconds < $this->limitTime) {
                $content =  "频繁提交,请稍候再试";
                $type = "html";
                header("Content-type: text/html; charset=utf-8");
                return Response::create($content, $type, 0);
            }
        }

        $cookie = cookie($k);
        if (empty($cookie)) {
            //保存到晚上12点
            $saveTime = strtotime(date("Y-m-d", time()) . " 23:59:59");
            setcookie($k, (string)time(), $saveTime, "/");
            $cookie = "first";
        } else {
            $cookie = "continue";
        }
        if (key_exists('fp', $_GET)) {
            $_GET['fp'];
        }
        $mobileText = "计算机端浏览器";
        if (is_mobile()) {
            $mobileText = "移动端浏览器";
        }
        //在搜索引擎搜索个关键词，进入网站
        $word = search_word_from();
        if (!empty($word['keyword'])) {
            echo '关键字：' . $word['keyword'] . ' 来自：' . $word['from'];
        }
        if (key_exists('fp', $_GET)) {
            $from_page = $_GET['fp'];
        } else {
            $from_page = getFromPage();
        }
        $page_title = getPageTitle($from_page); //网页标题
        if (empty($page_title)) {
            $page_title = "访问进入了";
        }
        $startTime = date("Y-m-d") . " 00:00:00";
        $endTime = date("Y-m-d") . " 23:59:59";
        $browser = getBrowser();
        $as = AccessStat::where([['ip', '=', $ip], ['from_page', '=', $from_page], ['browser', '=', $browser]])->whereBetweenTime("create_time", $startTime, $endTime)->find();
        if ($as) {
            $type = "html";
            $content = "'存在" . $cookie . "'";
            header("Content-type: text/html; charset=utf-8");
            return Response::create($content, $type, 200);
        }
        $accessStat = ["cookie" => $cookie, "ip" => $ip, "browser" => $browser, "from_page" => $from_page, "source_site" => $word['from'], "browser_type" => $mobileText, 'page_title' => $page_title];
        try {
            AccessStat::create($accessStat);
            saveToCache($k, time()); //记录一下时间
        } catch (\Exception $e) {
            $type = "html";
            $content = "'" . $e->getMessage() . "'";
            return Response::create($content, $type, 200);
        }
        $type = "html";
        $content = "'成功" . $cookie . "'";
        header("Content-type: text/html; charset=utf-8");
        return Response::create($content, $type, 200);
    }

    public function check()
    {
        return "check";
    }
}